﻿<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        Exception ex = Server.GetLastError().GetBaseException(); /* ROOT Error */
        Exception dispex = Server.GetLastError();
        
        int status = 500;
        if (ex is System.Threading.ThreadAbortException) return;
        else if (ex is HttpException)
        {
            status = ((HttpException)ex).GetHttpCode();
        }
        else if (ex is HttpUnhandledException)
        {
            status = ((HttpUnhandledException)ex).GetHttpCode();
        }

        /* Tampilkan error message yg tidak terlalu detail */
        string dispExMsg = dispex.Message;

        /* Get ROOT error message */
        string exType = ex.GetType().Name;
        string exMsg = ex.Message;
        string exStack = ex.StackTrace;
        string url = "UNKNOWN";
        string user = "SYSTEM";

        if (ConfigurationManager.AppSettings["_CONFIG_CLIENT_ERROR_MODE"] == "full") {
            if (dispExMsg!=exMsg) dispExMsg += exMsg;
        }
        
        
        if (HttpContext.Current != null)
        {
            url = HttpContext.Current.Request.Url.AbsolutePath;
            user = HttpContext.Current.User.Identity.Name;
        }
        string logid = Guid.NewGuid().ToString();
        Logging.WriteDB(logid, url, exType, exMsg, exStack, user);


        Response.StatusCode = status;
        Response.Write(@"<html>
    <head>
        <title>Error " + status + @"</title>
        <link href=""assets/css/error.css"" rel=""stylesheet"" />
        <script src=""assets/js/jquery/jquery.min.js""><"+@"/script>
        <script src=""assets/js/docs.js""><"+@"/script>
    </head>
    <body>
        <div class=""error-block"">
            <h1>Error " + status + @" </h1>
            <div class=""msgblock"">" + dispExMsg + @"</div>         
            <div class=""msgid"">Correlation Token : " + logid + @"</div>
            <div class=""buttons"">
            <input type=""button"" class=""btnSend"" value=""Report Error"" />
            </div>
        </div>                 
    </body>
</html>");
        Server.ClearError();
        
    }

    void Session_Start(object sender, EventArgs e)  
    {
        // Code that runs when a new session is started
        //HttpContext.Current.Session["userData"] = null;
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

    void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
    {

        // Get a reference to the current User
        System.Security.Principal.IPrincipal usr = HttpContext.Current.User;

        // If we are dealing with an authenticated forms authentication request
        if (usr.Identity.IsAuthenticated && usr.Identity.AuthenticationType == "Forms")
        {

            FormsIdentity fIdent = usr.Identity as FormsIdentity;

            // Create a CustomIdentity based on the FormsAuthenticationTicket  
            AHMIDEAIdentity ci = new AHMIDEAIdentity(fIdent.Ticket);

            // Create the CustomPrincipal
            AHMIDEAPrincipal p = new AHMIDEAPrincipal(ci);

            // Attach the CustomPrincipal to HttpContext.User and Thread.CurrentPrincipal
            HttpContext.Current.User = p;

            System.Threading.Thread.CurrentPrincipal = p;

        }
        else {
            if (HttpContext.Current.Request.Url.AbsolutePath.Contains("/apps/"))
            {
                Response.StatusCode = 401;
                Response.StatusDescription = "Session Expired";
                Server.ClearError();
            }
        }

    }
       
</script>
